<?php

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

require_once __DIR__.'/../../utils/checkTokenUtils.php';
require_once __DIR__.'/../../model/BookCategoryModel.php';
use model\BookCategoryModel;

// 读取Session中的提示信息
$successMessage = $_SESSION['success_message'] ?? null;
$errorMessage = $_SESSION['error_message'] ?? null;

// 初始化分类模型并获取分页数据
$categoryModel = new BookCategoryModel();
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$perPage = isset($_GET['per_page']) ? intval($_GET['per_page']) : 8;
$perPage = max(1, min($perPage, 100));
$paginationData = $categoryModel->getPaginatedCategories($currentPage, $perPage);
extract($paginationData);

ob_start();
?>

    <!-- 成功/错误提示 -->
    <?php if ($successMessage): ?>
        <div id="successNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-green-100 border border-green-400 text-green-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
            <?= htmlspecialchars($successMessage) ?>
        </div>
    <?php endif; ?>

    <?php if ($errorMessage): ?>
        <div id="errorNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-red-100 border border-red-400 text-red-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
            <?= htmlspecialchars($errorMessage) ?>
        </div>
    <?php endif; ?>

    <!-- 清除已显示的消息 -->
    <?php
    unset($_SESSION['success_message'], $_SESSION['error_message']);
    ?>

    <!-- 分类列表 -->
    <div class="container mt-8">
        <button id="addBookBtn" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mb-4 inline-block">新增分类</button>

        <table class="w-full">
            <thead>
                <tr class="bg-gray-100">
                    <th class="py-3">ID</th>
                    <th class="py-3">分类名称</th>
                    <th class="py-3">分类说明</th>
                    <th class="py-3">操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($categories as $category): ?>
                    <tr>
                        <td class="p-3 text-center"><?= htmlspecialchars($category['id']) ?></td>
                        <td class="p-3 text-center"><?= htmlspecialchars($category['category_name']) ?></td>
                        <td class="p-3 text-center"><?= htmlspecialchars($category['description']) ?></td>
                        <td class="p-3 text-center">
                            <button
                                    class="edit-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors mr-2"
                                    data-id="<?=htmlspecialchars($category['id'])?>"
                                    data-category-name="<?=htmlspecialchars($category['category_name'])?>"
                                    data-description="<?=htmlspecialchars($category['description'])?>"

                            >编辑</button>
                            <button class="delete-btn px-3 py-1 bg-red-500 text-white rounded-md hover:bg-red-600 transition-colors" data-delete-url="DeleteCategory.php?id=<?=htmlspecialchars($category['id'])?>">删除</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<!--引入分页模板-->
<?php include __DIR__ . '/../partials/pagination.php';?>
<!--引入添加分类模态框-->
<?php include __DIR__.'/partials/add_modal.php';?>
<!--引入编辑模态框-->
<?php include __DIR__.'/partials/edit_modal.php'?>
<!--引入删除提示框-->
<?php include __DIR__.'/../partials/del_modal.php';?>
<?php
$content = ob_get_clean();
require_once __DIR__.'/../layouts/Main.php';//引入主布局
?>